package com.sunny.ynhz.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunny.ynhz.R;
import com.sunny.ynhz.bean.IndexProblemListBean;
import com.sunny.ynhz.ui.daily.news.PatrolNewsActivity;
import com.sunny.ynhz.utils.TimeFormatUtils;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * Created by luao on 2017/8/18.
 */


public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> implements List<IndexProblemListBean.DataBean> {

    private List<IndexProblemListBean.DataBean> data = null;
    private Context context;
    private final Object lock = new Object();

    public TestAdapter(Context context, List<IndexProblemListBean.DataBean> data) {
        this.data = data;
        this.context = context;

    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_patrol_news, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        //取data的时候防止数组越界
        final IndexProblemListBean.DataBean dataBean = data.get(viewHolder.getLayoutPosition());
        viewHolder.title.setText(dataBean.getName());
        viewHolder.time.setText(TimeFormatUtils.timeStamp2Date(dataBean.getTm(), "[MM-dd HH:mm]"));


        viewHolder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, PatrolNewsActivity.class);
                intent.putExtra("type", dataBean.getType());
                intent.putExtra("id", dataBean.getId());
                intent.putExtra("url",dataBean.getUrl());
                context.startActivity(intent);
            }
        });
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return data.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView title;
        public TextView time;
        public LinearLayout content;

        public ViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            time = (TextView) view.findViewById(R.id.time);
            content = (LinearLayout) view.findViewById(R.id.content);
        }
    }

    @Override
    public void add(int location, IndexProblemListBean.DataBean object) {
        synchronized (lock) {
            data.add(location, object);
            notifyItemInserted(location);
        }
    }

    @Override
    public IndexProblemListBean.DataBean remove(int i) {
        return null;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator<IndexProblemListBean.DataBean> listIterator() {
        return null;
    }

    @NonNull
    @Override
    public ListIterator<IndexProblemListBean.DataBean> listIterator(int i) {
        return null;
    }

    @NonNull
    @Override
    public List<IndexProblemListBean.DataBean> subList(int i, int i1) {
        return null;
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @NonNull
    @Override
    public Iterator<IndexProblemListBean.DataBean> iterator() {
        return null;
    }

    @NonNull
    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @NonNull
    @Override
    public <T> T[] toArray(@NonNull T[] ts) {
        return null;
    }

    @Override
    public boolean add(IndexProblemListBean.DataBean object) {
        synchronized (lock) {
            int lastIndex = data.size();
            if (data.add(object)) {
                notifyItemInserted(lastIndex);
                return true;
            } else {
                return false;
            }
        }
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(@NonNull Collection<?> collection) {
        return false;
    }


    @Override
    public boolean addAll(int location, Collection<? extends IndexProblemListBean.DataBean> collection) {
        synchronized (lock) {
            if (data.addAll(location, collection)) {
//                notifyItemRangeInserted(location, collection.size());
                notifyItemRangeChanged(location + collection.size(), getItemCount()-collection.size());
                return true;
            } else {
                return false;
            }
        }
    }

    @Override
    public boolean removeAll(@NonNull Collection<?> collection) {
        return false;
    }

    @Override
    public boolean retainAll(@NonNull Collection<?> collection) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public IndexProblemListBean.DataBean get(int i) {
        return null;
    }

    @Override
    public IndexProblemListBean.DataBean set(int i, IndexProblemListBean.DataBean dataBean) {
        return null;
    }

    @Override
    public boolean addAll(Collection<? extends IndexProblemListBean.DataBean> collection) {
        synchronized (lock) {
            int lastIndex = data.size();
            if (data.addAll(collection)) {
                notifyItemRangeInserted(lastIndex, collection.size());

                return true;
            } else {
                return false;
            }
        }
    }
}